{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ee49cf13",
   "metadata": {},
   "source": [
    "lambda 函数是一个小的匿名函数。\n",
    "\n",
    "一个 lambda 函数可以接受任意数量的参数，但只能有一个表达式。\n",
    "\n",
    "# 语法\n",
    "lambda 参数：表达式\n",
    "\n",
    "执行表达式并返回结果："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "97752bad",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "15\n"
     ]
    }
   ],
   "source": [
    "x = lambda a : a + 10\n",
    "print(x(5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0ecc1fe2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "30\n"
     ]
    }
   ],
   "source": [
    "x = lambda a, b : a * b\n",
    "print(x(5, 6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "eed85b6b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "13\n"
     ]
    }
   ],
   "source": [
    "x = lambda a, b, c : a + b + c\n",
    "print(x(5, 6, 2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a782a819",
   "metadata": {},
   "source": [
    "# 为什么要使用 Lambda 函数？\n",
    "当您将它们用作另一个函数中的匿名函数时，可以更好地展示 lambda 的力量。\n",
    "\n",
    "假设您有一个接受一个参数的函数定义，并且该参数将乘以一个未知数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8395b957",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "22\n"
     ]
    }
   ],
   "source": [
    "def myfunc(n):\n",
    "  return lambda a : a * n\n",
    "\n",
    "mydoubler = myfunc(2)\n",
    "\n",
    "print(mydoubler(11))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "27585114",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "33\n"
     ]
    }
   ],
   "source": [
    "mytripler = myfunc(3)\n",
    "\n",
    "print(mytripler(11))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
